﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace carApp
{
    public partial class Goods : Form
    {
        public Goods()
        {
            InitializeComponent();
        }

        private void Goods_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            LoadData();
            
        }
        #region pager
        int pageSize = 20;
        int CurrentPage = 1;
        int pageCount = 0;
        DataTable dt;
        int CurrentRowIndex = 0;
        int CurrentColumnIndex = 0;
        void LoadData()
        {
            string where = "";
            if (txtKeyword.TextLength > 0)
                where = " name like'%" + txtKeyword.Text + "%'";
            int RowCount = db.count("goods",where);
            dt = db.GetPagerData("goods", "*", where, pageSize, CurrentPage);
            pageCount = (int)Math.Ceiling(RowCount * 1.0 / pageSize);
            lblPageCount.Text = "共 " + pageCount + " 页";
            txtPageCurrent.Text = CurrentPage.ToString();
            dataGridView1.DataSource = dt;
        }
        private void lblfirst_Click(object sender, EventArgs e)
        {
            CurrentPage = 1;
            LoadData();
        }

        private void lblprev_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            if (CurrentPage < 1)
                CurrentPage = 1;
            LoadData();
        }

        private void lblNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            if (CurrentPage > pageCount)
                CurrentPage = pageCount;
            LoadData();
        }

        private void lbllast_Click(object sender, EventArgs e)
        {
            CurrentPage = pageCount;
            LoadData();
        }
        private void btnPageGo_Click(object sender, EventArgs e)
        {
            CurrentPage = int.Parse(txtPageCurrent.Text);
            LoadData();
        }
        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var id = dataGridView1.Rows[CurrentRowIndex].Cells[0].Value;
            Model.Goods mod = new Model.Goods();
            mod = db.FillModel<Model.Goods>("goods", "id=" + id);
            if (mod != null)
            {
                txtSN.Text = mod.sn;
                txtColor.Text = mod.color;
                txtName.Text = mod.name;
                txtnum.Text = mod.stock.ToString();
                txtSpec.Text = mod.spec;
                txtSupply.Text = mod.supplyer;
                txtType.Text = mod.type;
                txtUnit.Text = mod.unit;
                lblId.Text = mod.id.ToString();
                panel_add.Visible = true;
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var id = dataGridView1.Rows[CurrentRowIndex].Cells[0].Value;
            SqliteHelper.ExecuteSql("delete from goods where id=" + id);
            this.dataGridView1.Rows.RemoveAt(CurrentRowIndex);
        }
        #endregion
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtName.TextLength == 0)
            {
                MessageBox.Show("请填写商品名称");
                return;
            }
            if (txtnum.TextLength==0)
            {
                MessageBox.Show("请填写入库数量");
                return;
            }           
            panel_add.Visible = false;
            try
            {
                if (lblId.Text == "0")
                {
                    if (db.count("goods", "sn='" + txtSN.Text + "'") > 0)
                    {
                        db.inc("goods", "sn='" + txtSN.Text + "'", "stock", int.Parse(txtnum.Text));
                    }
                    else
                    {
                        Model.Goods goods = new Model.Goods();
                        goods.color = txtColor.Text;
                        goods.name = txtName.Text;
                        goods.sn = txtSN.Text;
                        goods.sort = 0;
                        goods.spec = txtSpec.Text;
                        goods.stock = int.Parse(txtnum.Text);
                        goods.supplyer = txtSupply.Text;
                        goods.type = txtType.Text;
                        goods.unit = txtUnit.Text;
                        if (db.add(goods) > 0)
                        {
                            MessageBox.Show("入库成功");
                        }
                        else
                        {
                            MessageBox.Show("入库失败");
                        }
                    }
                    Model.Store store = new Model.Store();
                    store.color = txtColor.Text;
                    store.name = txtName.Text;
                    store.sn = txtSN.Text;
                    store.username = txtUsername.Text;
                    store.spec = txtSpec.Text;
                    store.num = int.Parse(txtnum.Text);
                    store.supplyer = txtSupply.Text;
                    store.type = txtType.Text;
                    store.unit = txtUnit.Text;
                    store.times = txtTimes.Text == "" ? DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") : txtTimes.Text;
                    db.add(store);
                }
                else
                {
                    Model.Goods goods = new Model.Goods();
                    goods.id = int.Parse(lblId.Text);
                    goods.color = txtColor.Text;
                    goods.name = txtName.Text;
                    goods.sn = txtSN.Text;
                    goods.sort = 0;
                    goods.spec = txtSpec.Text;
                    goods.stock = int.Parse(txtnum.Text);
                    goods.supplyer = txtSupply.Text;
                    goods.type = txtType.Text;
                    goods.unit = txtUnit.Text;
                    if (db.edit(goods) > 0)
                    {
                        MessageBox.Show("修改成功");
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                }
                LoadData();
            }
            catch(Exception ex)
            {
                ReportEx.upPortEX(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            txtSN.Text = "G"+DateTime.Now.ToString("yyyyMMddhhmmss");
            panel_add.Visible = true;
        }

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            var dgv = (DataGridView)sender;
            CurrentRowIndex = e.RowIndex;
            CurrentColumnIndex = e.ColumnIndex;
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                CurrentRowIndex = e.RowIndex;
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[1];
                contextMenuStrip1.Show(Cursor.Position);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            string where = "";
            if (txtKeyword.TextLength > 0)
                where = " name like'%" + txtKeyword.Text + "%'";
            dt = db.GetTable("goods", where, "sn,type,name,spec,unit,color,stock,out_stock,supplyer");
            if(dt.Rows.Count==0)
            {
                MessageBox.Show("没有数据");
                return;
            }
            var csvPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            if(csvPath=="")
            {
                csvPath = Application.StartupPath.TrimEnd();
            }
            var csvName = csvPath + "\\产品" +DateTime.Now.ToString("yyyyMMddHHmmss")+".csv";
            
            if (Libs.FileHelper.DataToCSV(dt, csvName, "编号,类型,产品名称,规格,单位,颜色,库存,出库量,供应商"))
            {
                MessageBox.Show("导出成功");
                System.Diagnostics.Process.Start("explorer.exe", csvPath + @"\");
            }
            else
            {
                MessageBox.Show("导出失败");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel_add.Visible = false;
        }
    }
}
